/*
 * ModuleArray.java
 * 
 * last update: 16.01.2010 by Stefan Saru
 * 
 * author:	Alec(panovici@elcom.pub.ro)
 * 
 * Obs:
 */

package engine;

import java.util.*;
import middle.ScopeNode;

/**
 * An array of module or UDP instances (_NOT_ gates !). 
 */

class ModuleArray extends NameSpace {

	Module[] array;
	int ars, are, n, increment;


	ModuleArray (NameSpaceDescription desc, NameSpace parent,
			int ars, int are, int n,
			int increment, Module[] array)
			throws ParseException
			{
		super(parent, desc);
		this.ars = ars;
		this.are = are;
		this.n = n;
		this.increment = increment;
		this.array = array;
			}

	/**
	 * Handles the indexes. resolveName need not to be redefined
	 * because the grammar should not allow an FQN to start
	 * with an index.
	 */
	public Object resolveNameExact(FQN fqn) {
		int index;
		try {
			index = (int) ((Expression) fqn.node).evaluate().getLong();
		} catch (InterpretTimeException iex) {
			return null;
		}
		//bring the index into the length:0 range
		//bugfix for inverse instantiation order:
		//we resolve indexes into the reversed order, so everything is OK
		index = increment == 1 ? index - ars : ars - index;

		if(index >= array.length) return null;
		if(fqn.next != null)
			return array[index].resolveNameExact(fqn.next);
		return array[index]; //i'm not sure when one may need a module name asa fqn...
	}
}